<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ page import="java.sql.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.nio.charset.StandardCharsets"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>saveOffline</title>
</head>
<body>
<%
//http://192.168.43.215:8080/MealPrj/customer/saveOffline.jsp?table-number=3&customer_name=marry&restaurant_name=LIU
//接收from数据
request.setCharacterEncoding("UTF-8");

String num = request.getParameter("table-number");//桌号
String customer_name = request.getParameter("customer_name");
String restaurant_name = request.getParameter("restaurant_name");

//连接数据库
try {
    Class.forName("org.mariadb.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost:3306/mealdb?user=root&password=1230321");
    Statement stmt = conn.createStatement();

    String sql1 = "UPDATE `table` SET state = '已预订' WHERE num = '" + num + "'";
    stmt.executeUpdate(sql1);

    String sql2 = "UPDATE `customers` SET table_num = '" + num + "' WHERE `name` = '" + customer_name + "'";
    stmt.executeUpdate(sql2);

    String encodedUrl = "restaurantIndex.jsp?customer_name=" + customer_name + "&restaurant_name=" + restaurant_name;

    // 重定向到餐厅首页
    response.sendRedirect(encodedUrl);

    // 关闭数据库连接
    stmt.close();
    conn.close();

} catch (ClassNotFoundException | SQLException e) {
    e.printStackTrace(); // 打印异常详情到控制台，用于调试
    // 可以在这里处理异常，例如重定向到错误页面
}
%>
</body>
</html>